﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Web.UI;
using Paging;

namespace KPIS.GERP.WEB.GFMS.Helpers.ExtensionMethods
{
    /// <summary>
    /// Sample of use =>
    // public ActionResult PagedProducts(int? page)
    //{
    //    var products = _entities.ProductSet
    //        .OrderBy(p => p.Id).ToPagedList(page, 2);
    //    return View(products);
    //}
    //
    //public ActionResult PagedSortedProducts(string sort, int? page)
    //{
    //    IQueryable<Product> products;
    //    sort = sort ?? string.Empty;
    //    switch (sort.ToLower())
    //    {
    //        case "name":
    //            products = from p in _entities.ProductSet
    //                       orderby p.Name
    //                       select p;
    //            break;
    //        case "price":
    //            products = from p in _entities.ProductSet
    //                       orderby p.Price
    //                       select p;
    //            break;
    //        default:
    //            products = from p in _entities.ProductSet
    //                       orderby p.Id
    //                       select p;
    //            break;
    //    }

    //    ViewData.Model = products.ToPagedList(page, 2, sort);
    //    return View();
    //}
    /// </summary>
    public class HelpersDataGridHelperPaging
    {
        private static void RenderPagerRow<T>(HtmlHelper helper, HtmlTextWriter writer, PagedList<T> items, int columnCount)
        {
            // Don't show paging UI for only 1 page
            if (items.TotalPageCount == 1)
                return;

            // Render page numbers
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.AddAttribute(HtmlTextWriterAttribute.Colspan, columnCount.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            var currentAction = (string)helper.ViewContext.RouteData.Values["action"];
            for (var i = 0; i < items.TotalPageCount; i++)
            {
                if (i == items.PageIndex)
                {
                    writer.Write(String.Format("<strong>{0}</strong>&nbsp;", i + 1));
                }
                else
                {
                    var linkText = String.Format("{0}", i + 1);
                    var link = helper.ActionLink(linkText, currentAction, new { page = i, sort = items.SortExpression });
                    writer.Write(link + "&nbsp;");
                }
            }
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

    }
}